require(['init'], function() {
  var $pagination = $('#js-pagination')
  var pagination
  //渲染页面
  var i = "";
  var query = {}
  //请求全部
  function reader_order(querystr, cb) {
    var url ="/api/orders"
    if(querystr) {
      query = querystr
    }
    ajaxMethod({
      url: url,
      data: query,
      method: "get",
      success: function(data) {
        cb && cb(data)
        loadData(data);
      }
    })
  }
  // 待支付
  function wating_order(querystr, cb) {
    var url ="/api/orders";
    var query = {}
    if(querystr) {


      query = querystr
    }
    query.status=1;
    ajaxMethod({
      url: url,
      data: query,
      method: "get",
      success: function(data) {
        cb && cb(data)
        loadData(data);
      }
    })
  }
  //已支付
  function paid_order(querystr, cb) {
    var url ="/api/orders";
    var query = {}
    if(querystr) {
      query = querystr
    }
    query.status=3;
    ajaxMethod({
      url: url,
      data: query,
      method: "get",
      success: function(data) {
        cb && cb(data)
        loadData(data);
      }
    })
  }
  function loadData(data) {
    if(data.list.length == 0) {
      $('#student_orderlist').html('<div style="margin-top: 50px;text-align: center;">无结果</div>');
      $pagination.hide()
      return;
    }
    var $student_order = $('#student_orderlist').html("");
    var $thead = $('<thead>\
          <tr class="enroll">\
            <th>'+i18next.t('table.no')+'</th>\
            <th>'+i18next.t('table.ordernumber')+'</th>\
            <th>'+i18next.t('table.coursename')+'</th>\
            <th>'+i18next.t('table.phone')+'</th>\
            <th>'+i18next.t('table.coursept')+'</th>\
            <th>'+i18next.t('table.createtime')+'</th>\
            <th>'+i18next.t('table.status')+'</th>\
            <th>'+i18next.t('table.money')+'</th>\
            <th>'+i18next.t('table.studentname')+'</th>\
            <th>'+i18next.t('table.handle')+'</th>\
          </tr>\
        </thead>').appendTo($student_order)
    var $tbody = $('<tbody></tbody>').appendTo($student_order);
    $.each(data.list, function(index, item) {
      var $tr = $('<tr class="enroll" style="position:relative"></tr>')
      $tr.append('<td>' + (index + 1) + '</td>');
      $tr.append('<td>' + item.id + '</td>');
      $tr.append('<td>' + (item.productname||"--") + '</td>');
      $tr.append('<td>' + (item.mobile||"--") + '</td>');
      $tr.append('<td>' + (item.type === 1 ? i18next.t('table.classclass') : i18next.t('table.aboutclass')) + '</td>');
      $tr.append('<td>' + item.created_at + '</td>');
      var $status = $('<td></td>').appendTo($tr);
      $tr.append('<td>' + item.amount + '</td>');
      $tr.append('<td>' + item.studentname + '</td>');
      var $operation = $('<td></td>').appendTo($tr);
      switch (item.status) {
        case 0:
          var $cancelled = $('<a style="color:red">'+i18next.t('table.canceled')+'</a>').appendTo($status);
          var $failurebyed = $('<a style="color:#398dee">'+i18next.t('table.canceled')+'</a>').appendTo($operation);
          break;
        case 1:
          $('<a class="text-danger">'+i18next.t('table.unpaid')+'</a>').appendTo($status)
          var $cancel = $('<button class="btn btn-danger btn-sm btn-inverse">'+i18next.t('table.cancel')+'</button>').appendTo($operation)
          $cancel.on('click', function () {
            ajaxMethod({
              url: '/api/order/'+ item.id +'/cancel',
              method: 'put',
              success: function () {
                location.reload()
              }
            })
          })
          break;
        case 2:
          var $for_payment = $('<a>'+i18next.t('table.inpayment')+'</a>').appendTo($status);
          $for_payment.parent().parent('tr').addClass("len");
          var $confirm = $('<button class="btn btn-primary btn-sm btn-inverse ml5">'+i18next.t('table.confirm')+'</button>').appendTo($operation)
          $confirm.on('click', function () {
            ajaxMethod({
              url: '/api/order/'+ item.id +'/pay',
              method: 'put',
              success: function (data) {
                location.reload();
              }
            })
          })
          var $cancel_order = $('<button class="btn btn-danger btn-sm btn-inverse">'+i18next.t('table.cancel')+'</button>').appendTo($operation);
          $cancel_order.on('click', function() {
            $.ajax({
              url: "/api/order/" + item.id + "/cancel",
              method: "put",
              success: function(data) {
                location.reload();
              }
            })
          })
          break;
        case 3:
          var $payment = $('<a style="color:#32ab9e">'+i18next.t('table.alreadypaid')+'</a>').appendTo($status);
          var $failurebyed = $('<a style="color:#398dee">'+i18next.t('table.alreadypaid')+'</a>').appendTo($operation);
          $payment.parent().parent('tr').addClass("byed");
          break;
        case 4:
          var $failureby = $('<a style="color:#398dee">'+i18next.t('table.placement')+'</a>').appendTo($status);
          var $failurebyed = $('<a style="color:#398dee">'+i18next.t('table.placement')+'</a>').appendTo($operation);
          break;
        case 5:
          var $expired1 = $('<a>'+i18next.t('table.overdue')+'</a>').appendTo($status);
          var $expired2 = $('<a>'+i18next.t('table.overdue')+'</a>').appendTo($operation);

          break;
      }
      $tbody.append($tr)
    })
    $pagination.show();
  }
  function loadSearchData(querystr) {
    if(querystr) {
      pagination.filter.productname = querystr;
      pagination.filter.id = querystr;
      pagination.filter.mobile = querystr;

      pagination.searchData()
    } else {
      pagination.reloadData()
    }
  }
  $('#J_orders_search').on('click', function() {
    var $input = $(this).prev();
    var querystr = $.trim($input.val());
    loadSearchData(querystr)
  })

  $('.searchInput').on('keyup', function(e) {
    if(e.keyCode == 13) {
      var querystr = $.trim($(this).val());
      loadSearchData(querystr)
    }
  })
  $(".all_order").on('click', function() {
    $(this).addClass("s_hover")
    $('.payment_etc').removeClass('s_hover');
    $('.payment_has').removeClass('s_hover');
    pagination = new Pagination($pagination, reader_order)
  })
  $(".payment_etc").on('click', function() {
    $(this).addClass("s_hover")
    $('.all_order').removeClass('s_hover');
    $('.payment_has').removeClass('s_hover');
    pagination = new Pagination($pagination, wating_order);
    
  })
  $(".payment_has").on('click', function() {
    $(this).addClass("s_hover")
    $('.all_order').removeClass('s_hover');
    $('.payment_etc').removeClass('s_hover');
    pagination = new Pagination($pagination, paid_order);
  })
  pagination = new Pagination($pagination, reader_order)
})
